package com.module.recall.dao;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import base.dao.GenericHibernateDao;
import base.util.Page;

import com.module.recall.vo.AntiFakeRecallReg;

public class AntiFakeRecallRegDao extends
		GenericHibernateDao<AntiFakeRecallReg, Long> {
	private static AntiFakeRecallRegDao antiFakeRecallRegDao;

	/**
	 * 获取扫码记录单例;
	 * 
	 * @return
	 */
	public synchronized static AntiFakeRecallRegDao getInstance() {
		if (antiFakeRecallRegDao == null) {
			antiFakeRecallRegDao = new AntiFakeRecallRegDao();
		}
		return antiFakeRecallRegDao;
	}

	/**
	 * 通过antiFakeProductRecall查询出列表
	 * 
	 * @param session
	 * @param obj
	 * @param page
	 * @return
	 */
	public Page getList(Session session, AntiFakeRecallReg obj, Integer start,
			Integer limit) {
		String whereStr = "";
		String hql = " FROM AntiFakeRecallReg obj ";
		String hqlCount = "SELECT count(*) FROM AntiFakeRecallReg obj ";
		String split = " WHERE ";
		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		if (StringUtils.isNotBlank(obj.getOrgid())) {
			whereStr += split + " obj.orgid='" + obj.getOrgid() + "' ";
			split = " AND ";
		}
		if (StringUtils.isNotBlank(obj.getCodeInfo())) {
			whereStr += split + " obj.codeInfo like '%" + obj.getCodeInfo()
					+ "%' ";
			split = " AND ";
		}
		if (StringUtils.isNotBlank(obj.getTel())) {
			whereStr += split + " obj.tel like '%" + obj.getTel() + "%' ";
			split = " AND ";
		}
		if (StringUtils.isNotBlank(obj.getUserName())) {
			whereStr += split + " obj.userName like '%" + obj.getUserName()
					+ "%' ";
			split = " AND ";
		}
		Query query = session.createQuery(hql + whereStr
				+ " ORDER BY obj.createtime DESC");
		query.setFirstResult(page.getStart()).setMaxResults(page.getLimit());
		page.setTotal((long) ((Long) session.createQuery(hqlCount + whereStr)
				.uniqueResult()).intValue());
		page.setRows(query.list());
		return page;
	}

}
